<?php
namespace app\common\controller;

trait Token{
    protected $member_id = 0;
    //需要用户验证用户身份
    protected function checkUserToken()
    {
        $post = request()->post();
        $user_token = $post['user_token']??'';
        $wxsoft = $post['wxsoft']??'beimei';
        $field_token = $wxsoft.'_token';
        if(empty($user_token)){
            $this->echoJs(['code'=>-101,'msg'=>'请先登录']);//代表用户未登录，需要重新登陆
        }
        $member = db("member")->where($field_token,$user_token)->find();
        if(empty($member)){
            $this->echoJs(['code'=>-102,'msg'=>'用户token有误']);//代表用户未登录，需要重新登陆
        }

        if($member[$field_token.'_time'] < time()){
            $this->echoJs(['code'=>-103,'msg'=>'token已过期']);//代表过期，需要重新刷取token
        }

        //更新token时间
        //$this->updateUserTokenTime($member['member_id']);
        $this->member_id = $member['member_id'];
    }

    //直接数据json数据
    protected function echoJs($data)
    {
        header('Content-type:application/json; charset=utf-8');
        echo json_encode($data);exit;
    }

    //刷新用户的token
    protected function updateUserTokenTime($member_id,$token_name='beimei_token')
    {
        db('member')->where('member_id',$member_id)->update([
            $token_name.'_time'=>time()+7*24*3600
//            'token_time'=>time()+10
        ]);
    }

    //验证sign
    protected function checkSign()
    {
        $post = request()->post();
        $sign = $post['sign']??'';//小程序接口token
        if(empty($sign)){
            echo $this->echoJs(['code'=>-1001,'msg'=>'缺少sign参数']);
        }
        $timestamp = $post['timestamp'];
        if(empty($timestamp)){
            echo $this->echoJs(['code'=>-1002,'msg'=>'缺少timestamp参数']);
        }
        //程序id和密钥
        $clientid = "7DC8EA68A71F60AA";
        $secret = "Kwib0n2ol9";

        unset($post['sign']);
        $post['clientid'] = $clientid;
        $post['secret'] = $secret;

        //判断时间是否过期 接口过期时间1分钟
        if(time()-$timestamp > 60){
            echo $this->echoJs(['code'=>-1003,'msg'=>'接口已过期']);
        }
        //排序
        ksort($post);
        //参数拼接 a=1&b=2
        $qurey = http_build_query($post);
        //验证签名
        if($sign!=md5($qurey)){
            echo $this->echoJs(['code'=>-1001,'msg'=>'签名错误']);
        }
    }

    public function getApiSign()
    {
        //程序id和密钥
        $clientid = "7DC8EA68A71F60AA";
        $secret = "Kwib0n2ol9";

        $post = request()->post();

        $post['clientid'] = $clientid;
        $post['secret'] = $secret;
        $post['timestamp'] = $post['timestamp']??time();
        //排序
        ksort($post);
        //参数拼接 a=1&b=2
        $qurey = http_build_query($post);
//        var_dump($qurey);exit;
        $post['sign'] = md5($qurey);

        unset($post['clientid']);
        unset($post['secret']);

        return json_encode($post);
    }

    //没有登陆返回空值
    protected function returnUserToken()
    {
        $post = request()->post();
        $user_token = $post['user_token']??'';
        $wxsoft = $post['wxsoft']??'beimei';
        $field_token = $wxsoft.'_token';
        if(!empty($user_token)){
            $member = db("member")->where($field_token,$user_token)->find();
            if(!empty($member)){
                $this->member_id = $member['member_id'];
            }
        }
    }
}